/*Module by:    Roman Savchenko
**Function:     To display the cars that come at the user
**              To draw the lines that sparate the lanes
**
**Comming Soon: Support for multiple levals (minimun speed increase)
**              Support for multiple difficulties (number of lanes increase)
**              Make lanes look like they move
**              Change lane width/ possition to make them more apealing
**              Replace the word "car" with 'graphical' cars
*/

#include <curses.h>
#include <stdlib.h>
#include "constants_genral.h"
#include "otherCars.h"
#include <unistd.h>

void PutCarInLane(int row, int col, int lane);
void DeleteCarInLane(int row, int col, int lane);

/*lane car is used by other_cars, it says which lane will not have a opponent car in it*/
//int lanecar;


/*Display cars that come at the main car*/
int  other_carsEasy(int laneSpeed, WINDOW *wnd, int row)
{
        int     maxcol=COL,
                maxrow=ROW,
                col=0;
               
//      getmaxyx(wnd, maxrow, maxcol);  /*Gets the size of the window*/
        col = (maxcol/LANESTART)+3;     /*set where the first colum starts*/
       
        if (row >= maxrow || row ==0)
        {
                row = 0;
                srand(time(NULL));      /*set time seed to zero for random number*/
                lanecar = (rand() % 3 +1);      /*get random number between 1 and 3*/
                       
        }
        switch(lanecar)
        {
                case 1:         /*dont put car in lane 1*/
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        break;
                case 2:         /*dont put car in lane 2*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 3);
                        break;
                case 3:         /*dont put car in lane 3*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        break;
        }
                               
        refresh();
        usleep(laneSpeed);      /*speed */
       
        DeleteCarInLane(row, col, 1);
        DeleteCarInLane(row, col, 2);
        DeleteCarInLane(row, col, 3);
        refresh();
        row +=1;
                       
        return row;
}

int  other_carsMedium(int laneSpeed, WINDOW *wnd, int row)
{
        int     maxcol=COL,
                maxrow=ROW,
                col=0;

//        getmaxyx(wnd, maxrow, maxcol);  /*Gets the size of the window*/
        col = (maxcol/LANESTART)+3;     /*set where the first colum starts*/

        if (row >= maxrow || row ==0)
        {
                row = 0;
                srand(time(NULL));      /*set time seed to zero for random number*/
                lanecar = (rand() % 4 +1);      /*get random number between 1 and 4*/

        }
        switch(lanecar)
        {
                case 1:         /*dont put car in lane 1*/
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 4);
                        break;
                case 2:         /*dont put car in lane 2*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 4);
                        break;
                case 3:         /*dont put car in lane 3*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 4);
                        break;
                case 4:         /*dont put car in lane 4*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        break;
        }

        refresh();
        usleep(laneSpeed);      /*speed */

        DeleteCarInLane(row, col, 1);
        DeleteCarInLane(row, col, 2);
        DeleteCarInLane(row, col, 3);
        DeleteCarInLane(row, col, 4);
        refresh();
        row +=1;

        return row;
}

int  other_carsHard(int laneSpeed, WINDOW *wnd, int row)
{
        int     maxcol=COL,
                maxrow=ROW,
                col=0;

//        getmaxyx(wnd, maxrow, maxcol);  /*Gets the size of the window*/
        col = (maxcol/LANESTART)+3;     /*set where the first colum starts*/

        if (row >= maxrow || row ==0)
        {
                row = 0;
                srand(time(NULL));      /*set time seed to zero for random number*/
                lanecar = (rand() % 5 +1);      /*get random number between 1 and 5*/

        }
        switch(lanecar)
        {
                case 1:         /*dont put car in lane 1*/
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 4);
                        PutCarInLane(row, col, 5);
                        break;
                case 2:         /*dont put car in lane 2*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 4);
                        PutCarInLane(row, col, 5);
                        break;
                case 3:         /*dont put car in lane 3*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 4);
                        PutCarInLane(row, col, 5);
                        break;
                case 4:         /*dont put car in lane 4*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 5);
                        break;
                case 5:         /*dont put car in lane 5*/
                        PutCarInLane(row, col, 1);
                        PutCarInLane(row, col, 2);
                        PutCarInLane(row, col, 3);
                        PutCarInLane(row, col, 4);
        }

        refresh();
        usleep(laneSpeed);      /*speed */

        DeleteCarInLane(row, col, 1);
        DeleteCarInLane(row, col, 2);
        DeleteCarInLane(row, col, 3);
        DeleteCarInLane(row, col, 4);
        DeleteCarInLane(row, col, 5);
        refresh();
        row +=1;

        return row;
}



/*Draws lanes, speed depnds of the laneSpeed varible,
        minimum laneSpeed changes with leval*/
int drawLanesEasy(int laneSpeed, WINDOW *wnd, int row)
{
          int   maxcol=COL,
                maxrow=ROW,
                col,
                rowTemp;

       // getmaxyx(wnd, maxrow, maxcol);          /*Gets the size of the window*/
        col= (maxcol/LANESTART);                                /*col is where the fist col will be printed*/
        if (row >= maxrow)
        {
                row = rowTemp;
                rowTemp-= 2;
                if (rowTemp >= maxrow)
                {
                        rowTemp = 0;
                }
        }
       
        /*print the lanes*/
        mvprintw(row, col, "|\n \n");
        mvprintw(row, col+ LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 2*LANEWIDTH, "|\n \n");        
        mvprintw(row, col+ 3*LANEWIDTH, "|\n \n");      
        refresh();
        usleep(10);            
       
        return row;     /*returns the row it was last on, to pass back in to its self" */

}
int drawLanesMedium(int laneSpeed, WINDOW *wnd, int row)
{
          int   maxcol=COL,
                maxrow=ROW,
                col,
                rowTemp;

    //    getmaxyx(wnd, maxrow, maxcol);          /*Gets the size of the window*/
        col= (maxcol/LANESTART);                                /*col is where the fist col will be printed*/
        if (row >= maxrow)
        {
                row = rowTemp;
                rowTemp-= 2;
                if (rowTemp >= maxrow)
                {
                        rowTemp = 0;
                }
        }

        /*print the lanes*/
        mvprintw(row, col, "|\n \n");
        mvprintw(row, col+ LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 2*LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 3*LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 4*LANEWIDTH, "|\n \n");
        refresh();
        usleep(10);

        return row;     /*returns the row it was last on, to pass back in to its self" */

}

int drawLanesHard(int laneSpeed, WINDOW *wnd, int row)
{
          int   maxcol=COL,
                maxrow=ROW,
                col,
                rowTemp;

       // getmaxyx(wnd, maxrow, maxcol);          /*Gets the size of the window*/
        col= (maxcol/LANESTART);                                /*col is where the fist col will be printed*/
        if (row >= maxrow)
        {
                row = rowTemp;
                rowTemp-= 2;
                if (rowTemp >= maxrow)
                {
                        rowTemp = 0;
                }
        }

        /*print the lanes*/
        mvprintw(row, col, "|\n \n");
        mvprintw(row, col+ LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 2*LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 3*LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 4*LANEWIDTH, "|\n \n");
        mvprintw(row, col+ 5*LANEWIDTH, "|\n \n");
        refresh();
        usleep(10);

        return row;     /*returns the row it was last on, to pass back in to its self" */

}


void PutCarInLane(int row, int col, int lane)
{
        mvprintw(row-1, col+ ((lane-1)*LANEWIDTH), "(|  |)");   /*put car in lane 3*/
        mvprintw(row, col+ ((lane-1)*LANEWIDTH), " |  |");
        mvprintw(row+1, col+ ((lane-1)*LANEWIDTH), "(|  |)");
}

void DeleteCarInLane(int row, int col, int lane)
{
        mvprintw(row-1, col+((lane-1)*LANEWIDTH), "      ");       /*Delete car in lane 2*/
        mvprintw(row, col+((lane-1)*LANEWIDTH), "     ");
        mvprintw(row+1, col+((lane-1)*LANEWIDTH), "      ");
}

void intro_car()
{
	int i;

	curs_set(0);

	mvprintw(19,5, "      THE");
	mvprintw(20,5, "      *******   ***    ***   ***   ***   ***   **  **  ******  ******  ****  ");
	mvprintw(21,5, "      *******  *****   ***   ***   ***   ***   **  **  ******  ******  ***** ");
	mvprintw(22,5, "      ***      ** **   **** ****   **** ****   **  **    **    ***     **  **");
	mvprintw(23,5, "      ***      ** **   ** *** **   ** *** **   **  **    **    *****   ***** ");
	mvprintw(24,5, "      ***      ** **   **  *  **   **  *  **   **  **    **    *****   ****  ");
	mvprintw(25,5, "      ***      ** **   **     **   **     **   **  **    **    ***     **  **");
	mvprintw(26,5, "      *******  *****   **     **   **     **   ******    **    ******  **  **");
	mvprintw(27,5, "      *******   ***   ***     *** ***     ***   ****     **    ******  **  ***");
	refresh();
	
	for(i = 4; i < 46; i++)
	{
		mvprintw(3,i, "                                         _._						");
		mvprintw(4,i, "                                _         -_\"=-._					");
		mvprintw(5,i, "   ..__     _______.---\"\"\"\"\"\"\"|| |          -_   \"=-._				");
		mvprintw(6,i, "  '__  \"\"\"\"\"       \"\"\"\"\" \"----''_`,,,,,,,,,,,,-______.-\"\"\"\"===.___		");
		mvprintw(7,i, "   |__]        __            o                   ',        __     \"\"--.__	");
		mvprintw(8,i, " :=======.--\"\"  \"\"--.=================================.--\"\"  \"\"--.=======\"\"-__	");
		mvprintw(9,i, " : [w]  : /        | :    |========================| : /       | : [w]        [	");
		mvprintw(10,i,"   -_   :|          |:    |========================| :|          |:___________V	");
		mvprintw(11,i,".   \"-__: |        / :_____|=======================|_: |        / :__________V	");
		mvprintw(12,i,".          \"\"____\"\"  '-------------------------------'  \"\"____\"\"  '-----------		");
		mvprintw(3,4, " THE COMMUTER");

		refresh();
		usleep(30000);
	}
	mvprintw(14,3,"						Press ENTER to continue...		");       
	refresh();
	while((i = getch()) != 10) {}
	mvprintw(14,3,"                                                                                 ");
	refresh();
}

void menu_screen()
{
	mvprintw(19,5, "      THE");
	mvprintw(20,5, "      *******   ***    ***   ***   ***   ***   **  **  ******  ******  ****  ");
	mvprintw(21,5, "      *******  *****   ***   ***   ***   ***   **  **  ******  ******  ***** ");
	mvprintw(22,5, "      ***      ** **   **** ****   **** ****   **  **    **    ***     **  **");
	mvprintw(23,5, "      ***      ** **   ** *** **   ** *** **   **  **    **    *****   ***** ");
	mvprintw(24,5, "      ***      ** **   **  *  **   **  *  **   **  **    **    *****   ****  ");
	mvprintw(25,5, "      ***      ** **   **     **   **     **   **  **    **    ***     **  **");
	mvprintw(26,5, "      *******  *****   **     **   **     **   ******    **    ******  **  **");
	mvprintw(27,5, "      *******   ***   ***     *** ***     ***   ****     **    ******  **  ***");
	
	mvprintw(3,45, "                                         _._						");
	mvprintw(4,45, "                                _         -_\"=-._					");
	mvprintw(5,45, "   ..__     _______.---\"\"\"\"\"\"\"|| |          -_   \"=-._				");
	mvprintw(6,45, "  '__  \"\"\"\"\"       \"\"\"\"\" \"----''_`,,,,,,,,,,,,-______.-\"\"\"\"===.___		");
	mvprintw(7,45, "   |__]        __            o                   ',        __     \"\"--.__	");
	mvprintw(8,45, " :=======.--\"\"  \"\"--.=================================.--\"\"  \"\"--.=======\"\"-__	");
	mvprintw(9,45, " : [w]  : /        | :    |========================| : /       | : [w]        [	");
	mvprintw(10,45,"   -_   :|          |:    |========================| :|          |:___________V	");
	mvprintw(11,45,"    \"-__: |        / :_____|=======================|_: |        / :__________V	");
	mvprintw(12,45,"           \"\"____\"\"  '-------------------------------'  \"\"____\"\"  '-----------		");
	mvprintw(3,4, " THE COMMUTER");
	refresh();
}
